package com.tencreat.mall.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.domain.mall.MallMessageInform;
import com.tencreat.domain.mall.dto.MallMessageInformApiOrderDTO;
import com.tencreat.domain.mall.vo.MallMessageInformApiOrderVO;
import com.tencreat.domain.mall.vo.MallMessageInformApiSystemVO;
import com.tencreat.service.mall.IMallMessageInformService;
import com.tencreat.service.mall.IMallMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息中心
 *
 * @author zixin
 */
@RequestMapping("/api/mall/message-inform")
@RestController
@Api(tags = "99-消息中心")
public class MallMessageInformApiController extends BaseController {

    @Autowired
    private Environment environment;

    @Autowired
    private IMallMessageInformService mallMessageInformService;
    @Autowired
    private IMallMessageService messageService;


    @ApiOperation("获取未读消息")
    @GetMapping("/unread")
    @ApiImplicitParam(value = "0|订单消息通知,1|系统消息", name = "type")
    HttpResult<Integer> unread(Integer type) {
        Integer unread = mallMessageInformService.unread(type);
        if (type == null) {
            // 聊天消息数
            Integer unreadMessage = messageService.getAppUnreadMessage();
            if (unreadMessage != null) {
                unread = unread + unreadMessage;
            }
        }

        return HttpResult.ok(unread);
    }

    @ApiOperation("清除未读消息")
    @GetMapping("/clear-unread")
    @ApiImplicitParam(value = "0|订单消息通知,1|系统消息", name = "type")
    HttpResult clearUnread(Integer type) {
        mallMessageInformService.clearUnread(type);
        return HttpResult.ok();
    }

    @ApiOperation("查看系统消息列表")
    @GetMapping("/system")
    HttpResult<List<MallMessageInformApiSystemVO>> system() {
        return HttpResult.ok(mallMessageInformService.system());
    }

    @ApiOperation("查看订单消息列表")
    @PostMapping("/order")
    HttpResult<List<MallMessageInformApiOrderVO>> order(@RequestBody MallMessageInformApiOrderDTO baseEntity) {
        Page<MallMessageInformApiOrderDTO> page = new Page<>(baseEntity.getPageNum(), baseEntity.getPageSize());
        return HttpResult.ok(mallMessageInformService.order(page));
    }

    @ApiOperation(value = "发送注册成功消息", hidden = true)
    @GetMapping("/register-success/{uuid}")
    HttpResult registerSuccess(@PathVariable Long uuid) {
        System.out.println(uuid);
        mallMessageInformService.addRegisterSuccess(uuid);
        return HttpResult.ok();
    }

    @ApiOperation("修改地址")
    @GetMapping("/updateOrderAddress")
    HttpResult updateOrderAddress(@ApiParam("消息ID") @RequestParam(required = true) Long id, @ApiParam("地址ID") @RequestParam(required = true) Long addressId) {
        mallMessageInformService.updateOrderAddress(id, addressId);
        return HttpResult.ok();
    }

    @ApiOperation("确认")
    @GetMapping("/confirm")
    HttpResult confirm(@ApiParam("消息ID") @RequestParam(required = true) Long id) {
        mallMessageInformService.lambdaUpdate().eq(MallMessageInform::getId, id)
                .set(MallMessageInform::getConfirm, 1)
                .set(MallMessageInform::getTitle, "订单信息确认成功")
                .update();

        return HttpResult.ok();
    }
}
